package Test;

import java.util.Scanner;
/**
 * 方法版ATM
 */
public class Tset1 {
    static String[][] users = {
            //用户信息
            {"aaaaaa", "111111", "1000"},
            {"bbbbbb", "222222", "2000"},
            {"cccccc", "333333", "3000"},
            {"dddddd", "444444", "4000"},
            {"eeeeee", "555555", "5000"}
    };
    static Scanner sc = new Scanner(System.in);

    static int index = 0;
    //保存成功时，用户数组下标

    public static void process() {
        //控制ATM流程
        System.out.println("欢迎使用lovo银行");
        while (true) {
            login();
            menu();
        }
    }

    public static void login() {
        //登录
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入账号：");
            String account = sc.next();
            System.out.println("请输入密码：");
            String pwd = sc.next();
            for (int j = 0; j < users.length; j++) {
                if (users[j][0].equals(account) && users[j][1].equals(pwd)) {
                    System.out.println("登录成功");
                    index = j;
                    return;
                }
            }
            System.out.println("账号或密码错误,请重新登录，还剩" + (2 - i) + "次");
        }
        System.out.println("次数用完，卡已被吞！");
        System.exit(0); //系统结束
    }

    public static void menu() {
        //菜单
        while (true) {
            System.out.println("1.存款        2.取款");
            System.out.println("3.查询        4.转账");
            System.out.println("5.切换账号     6.退出");
            System.out.println("7.修该密码");
            System.out.println("请选择：");
            int input = sc.nextInt();
            if (input == 1) {
                save();
            } else if (input == 2) {
                get();
            } else if (input == 3) {
                search();
            } else if (input == 4) {
                transfer();
            } else if (input == 5) {
                System.out.println("账号退出");
                return;
            } else if (input == 6) {
                System.out.println("谢谢使用！");
                System.exit(0);
            } else if (input == 7){
                modify();
            } else {
                System.out.println("输入错误，请重新输入：");
            }
        }

    }

    public static void save() {
        //存款
        System.out.println("请输入你要存储的金额：");
        double money = sc.nextDouble();
        users[index][2] = Double.parseDouble(users[index][2]) + money + "";
        System.out.println("存款成功!");
    }

    public static void get() {
        //取款
        System.out.println("请输入要取出的金额：");
        double money = sc.nextDouble();
        if (money > Double.parseDouble(users[index][2])) {
            System.out.println("余额不足，请重新输入：");
        } else {
            users[index][2] = Double.parseDouble(users[index][2]) - money + "";
            System.out.println("取款成功！");
        }
    }

    public static void search() {
        //查询
        System.out.println("您的余额为：" + users[index][2]);
    }

    public static void transfer() {
        //转账

        System.out.println("请输入转账账号：");
        String zz = sc.next();
        if(zz.equals(users[index][0])){
            System.out.println("不能转账给自己");
            return;
        }
        for (int i = 0; i < users.length; i++) {
            if (users[i][0].equals(zz)) {
                System.out.println("转账账号正确");
                System.out.println("请输入转入金额");
                double money = sc.nextDouble();
                if (money > Double.parseDouble(users[index][2])) {
                    System.out.println("余额不足");
                    return;
                } else {
                    users[index][2] = Double.parseDouble(users[index][2]) - money + "";
                    users[i][2] = Double.parseDouble(users[i][2]) + money + "";
                    System.out.println("转账成功,进入菜单");
                    menu();
                }
            }
        }
        System.out.println("转账账号错误");

    }
    public static void modify(){
        System.out.println("请输入旧密码：");
        String pwd1 = sc.next();
        String [] a = new String[2];
        if (pwd1.equals(users[index][1])){
            for (int i = 0; i < 2; i++){
                System.out.println("请输入第" + (i+1) + "次新密码");
                String pwd2 = sc.next();
                if (pwd2.equals(users[index][1])){
                    System.out.println("新密码不能和旧密码相同,请重新输入");
                    return;
                }
                a[i] = pwd2;
                if (i == 1){
                    if (a[0].equals(a[1])){
                        System.out.println("密码修改成功");
                        users[index][1] = a[0];
                        return;
                    }else {
                        System.out.println("密码修改错误");
                        return;
                    }
                }
            }
        }
        System.out.println("密码输入错误");
    }
    public static void main(String[] args) {
        process();
    }
}
